package com.mqqzt.douban.util;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;

public class LoadImageAsyncTask extends AsyncTask<String, Void, Bitmap> {
	LoadImageCallback loadImageCallback;
	
	public LoadImageAsyncTask(LoadImageCallback loadImageCallback) {
		this.loadImageCallback = loadImageCallback;
	}

	public interface LoadImageCallback{
		public void beforeLoadImage();
		public void afterLoadImage(Bitmap bitmap);
	}
	@Override
	protected void onPreExecute() {
		loadImageCallback.beforeLoadImage();
		super.onPreExecute();
	}

	@Override
	protected void onPostExecute(Bitmap result) {
		loadImageCallback.afterLoadImage(result);
		super.onPostExecute(result);
	}

	@Override
	protected Bitmap doInBackground(String... params) {
		try {
			String path = params[0];
			URL url = new URL(path);
			HttpURLConnection conn =  (HttpURLConnection) url.openConnection();
			InputStream is = conn.getInputStream();
			return BitmapFactory.decodeStream(is);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

}
